#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import os
import json

from common.parse_parafile import ParamFileUtil
from common.const import RepositoryDataTypeEnum

from adds.comm.log import log
from adds.comm.param_base import ParamBase


class RestoreParam(ParamBase):
    def __init__(self, req_id):
        super().__init__(req_id)
        self.parse_param()

    @staticmethod
    def get_data_path(repositories, repository_type):
        path = []
        for repository in repositories:
            if repository['repositoryType'] == repository_type:
                path = repository.get("path", [])
                break
        return path[0] if path else ""

    def get_repository(self, repository_type):
        repositories = self.get_copies()[0].get("repositories", [])
        for repository in repositories:
            if repository['repositoryType'] == repository_type:
                return repository
        return {}

    def get_repository_path(self, repository_type):
        repositories_path_list = self.get_repository(repository_type).get("path", [])
        return repositories_path_list[0] if repositories_path_list else ""

    def parse_param(self):
        """
        备份恢复参数结构:
        """
        try:
            self._param = ParamFileUtil.parse_param_windows_file(self.req_id)
        except Exception as err:
            raise Exception(f"Failed to parse job param file for {err}") from err
        if not self._param:
            raise Exception(f"Failed to parse job param file is none")

    def get_param(self):
        return self._param

    def get_copies(self):
        copies = self._param.get("job", {}).get("copies", [])
        if not copies:
            raise Exception(f"The copies value in the param file is empty or does not exist")
        return copies

    def get_object_info(self):
        object_from = self._param.get("job", {}).get("extendInfo", {}).get("objectFrom", '0')
        object_infos = self._param.get("job", {}).get("extendInfo", {}).get("objectInfo", "[]")
        log.info(f"get restore ADObjects info: {object_infos}")
        object_list = eval(object_infos)
        return object_list, object_from

    def get_restore_type(self):
        restore_type = self._param.get("job", {}).get("jobParam", {}).get("restoreType", 1)
        return restore_type

    def get_backup_path(self):
        backup_path = ""
        copy = self.get_copies()[0]
        target_path = copy.get("extendInfo", {}).get("backupTarget", "")
        path_id = target_path.split("\\")[-1]
        repository = self.get_repository(RepositoryDataTypeEnum.DATA_REPOSITORY.value)
        host_list = repository.get("remoteHost", [])
        remote_name = repository.get("remoteName", "")
        if host_list:
            ip = host_list[0].get("ip", "")
            backup_path = f"\\\\{ip}\\{remote_name}"
        log.info(f"Get backupTarget path: {backup_path}")
        return backup_path
